package com.crh2a

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Window
import android.view.WindowManager
import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.Button
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.crh2a.adapter.RecyclerviewAdapter
import com.crh2a.http.ILockAPI
import com.crh2a.models.Lock

import kotlinx.serialization.json.Json
import kotlinx.serialization.encodeToString
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory

class MainActivity : AppCompatActivity() {
    private var lockList: List<Lock>?=null
    private var host="http://192.168.1.200"

    @SuppressLint("SetJavaScriptEnabled", "MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // remove title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main)

//        val wv=findViewById<WebView>(R.id.wv)
        //动态创建组件加载html
        val wv=WebView(this);
        wv.settings.javaScriptEnabled = true
        wv.loadUrl("${host}/lightmini.html")

        //设置recyclerview
        var recyclerview = findViewById<RecyclerView>(R.id.recyclerview)
//        recyclerview.layoutManager=LinearLayoutManager(this)
//        recyclerview.adapter= lockList?.let { RecyclerviewAdapter(it) }

        //数据请求
        val retrofit= Retrofit.Builder()
            .baseUrl("${host}:3000/")
            //没有这个不能获取http返回的字符串
            .addConverterFactory(ScalarsConverterFactory.create())
            //解析json字符串
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        val api=retrofit.create(ILockAPI::class.java)
        api.locks().enqueue(object: Callback<List<Lock>> {
            override fun onResponse(call: Call<List<Lock>>, response: Response<List<Lock>>) {
//                println(response.body())
                lockList = response.body()
                lockList?.let {
//                    recyclerview.layoutManager=LinearLayoutManager(baseContext)
                    recyclerview.layoutManager=StaggeredGridLayoutManager(7,StaggeredGridLayoutManager.VERTICAL)
                    recyclerview.adapter= RecyclerviewAdapter(it,wv)
                }

            }
            override fun onFailure(call: Call<List<Lock>>, t: Throwable) {
                println("出错了")
            }
        })

//        //测试按钮
//        val btn=findViewById<Button>(R.id.btn)
//        btn.setOnClickListener {
////            wv.evaluateJavascript("console.log('abc1333')"){}
//            lockList?.let {
//                if(it.isNotEmpty()){
//                    val str= Json.encodeToString<Lock>(it[0])
////                    //字符串转对象
////                    val lock=Json.decodeFromString<Lock>("""{"id":"2","BlId":2,"DeviceId":"001","mainboardno":1,"lockno":2,"czh":"","occupied":false}""")
////                    print(lock.czh)
//                    wv.evaluateJavascript("javascript:openDoor('$str')"){
//
//                    }
//                }
//            }

        }

    }
